#include <stdio.h>
#include <stdlib.h>
#include <csp_spu.h>
#include <csp_debug.h>

#include "../alting_test.h"


#define WAIT_ITERATIONS 10000000


int global_pid; 


int Run(){

	int pid = csp_pid();

	global_pid = csp_pid();

	csp_process	writer_a2o_proc; 
	csp_process	writer_o2o_1_proc; 
	csp_process	writer_o2o_2_proc; 
	csp_process	writer_o2o_3_proc; 
	csp_process	writer_o2o_4_proc; 

	writer_a2o_proc   = csp_create_process("alting_test_writer_a2o_process"  );
	writer_o2o_1_proc = csp_create_process("alting_test_writer_o2o_1_process");
	writer_o2o_2_proc = csp_create_process("alting_test_writer_o2o_2_process");
	writer_o2o_3_proc = csp_create_process("alting_test_writer_o2o_3_process"); 
	writer_o2o_4_proc = csp_create_process("alting_test_writer_o2o_4_process");

	int n_procs = 5;
	printf("slow_starter runs %i processes that writes on a shared channel.\n", n_procs);

	/* This only starts processes that write to a shared channel. */
	csp_process writing_procs[] = {
		/* 5 processes that writes to the same ANY2ONE-channel. */
		writer_a2o_proc, 
		writer_a2o_proc,
		writer_a2o_proc,
		writer_a2o_proc,
		writer_a2o_proc,

		/* 4 ONE2ONE writing processes. */
		/*
		writer_o2o_1_proc, 
		writer_o2o_2_proc,
		writer_o2o_3_proc,
		writer_o2o_4_proc*/
	};


	int i, n;
	for(i=0; i < WAIT_ITERATIONS; i++){
		for(n=0; i < WAIT_ITERATIONS; i++){
			// do nothin;
		}
	}
	printf("slow starter task stopped waiting: "
			"execute %i parallel writers.\n", n_procs);

	ASSERT( !csp_run_parallel(writing_procs, n_procs) );

	PRINTF_FOR_TESTING("%s terminating on spe #%i with pid %i\n",__FILE__ , 
			csp_spe_id(), pid);

	PRINTF_FOR_TESTING("Nested task with pid=%i terminated with res=%i.\n", 
			pid, res);

	printf("der!\n");

	return 0;
}
